package com.TCP_UDP网络编程.UDP_网络编程;

import java.io.IOException;
import java.net.*;
@SuppressWarnings("all")
public class UDP01_Send发送端 {
    public static void main(String[] args) throws IOException {
        /// 1.发送
        DatagramSocket socket = new DatagramSocket(9998);
        byte[] bytes = "泥马勒戈壁的".getBytes();
        DatagramPacket packet =                  /// 装包，将内容发给指定主机和端口
                new DatagramPacket(bytes, bytes.length, InetAddress.getLocalHost(), 9999);
        socket.send(packet);                    ///发送
        System.out.println("发送成功~");

        /// 2.接受
        byte[] bytes1 = new byte[1024];         ///用来接收的数组
        DatagramPacket packet1 = new DatagramPacket(bytes1, bytes1.length);
        socket.receive(packet1);                ///接受另一个主机的内容给到packet1中，

        /// 3.拆包
        byte[] data = packet1.getData();
        int length = packet1.getLength();
        System.out.println(new String(data,0,length));

        /// 4.关闭资源
        socket.close();

    }

}
